package cn.calm.fileManager.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import cn.calm.commons.action.BaseAction;
import cn.calm.commons.entity.BaseEntity;
import cn.calm.commons.util.FileUtils;
import cn.calm.fileManager.entity.TreeNode;

public class FileManagerAction<T extends BaseEntity> extends BaseAction<T> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String CUTFILE = "CUTFILE";
	public static final String COPYFILE = "COPYFILE";
	private String root;
	private String name;
	private List<TreeNode> treeNodes;
	private List<File> file;
	private List<String> fileFileName;
	private List<String> fileContentType;
	private File rootFile = new File(getServletContext().getRealPath("/"));
	private String selects[];

	/**
	 * 
	 * 
	 * @return 执行结果
	 */
	public String uploadFile() {
		// HttpSession session = getHttpSession();
		//
		String uploadDir = getServletContext().getRealPath(root);
		//
		if (fileFileName == null) {
			addActionError("EMFM0002");
			return INPUT;
		}
		for (int i = 0; i < fileFileName.size(); i++) {
			String pathname = uploadDir + File.separator + fileFileName.get(i);
			File destFile = new File(pathname);
			if (destFile.exists()) {
				addActionError("EMFM0001", null, fileFileName.get(i));
			} else {
				try {
					FileUtils.copyFile(file.get(i), destFile);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return "uploadFile";
	}

	/**
	 * 
	 * 
	 * 
	 * 
	 * @return 执行结果
	 */
	public String delete() {
		if (selects == null) {
			addActionError("EMFM0003");
		} else {
			for (String s : selects) {
				String uploadDir = getServletContext().getRealPath(
						root + File.separator + s);
				File file = new File(uploadDir);
				deleteFile(file);
			}
		}
		return "delete";
	}
	
	public String makeDir(){
		File temp = new File(getServletContext().getRealPath(root)+File.separator + name);
		if(temp.exists()){
			addActionError("EMFM0001", null, name);
		}else{
			temp.mkdirs();
		}
		
		return "makeDir";
	}
	
	/**
	 * 
	 * 
	 * 
	 * @param file
	 */
	private void deleteFile(File file) {
		if (file.exists()) {
			if (file.isFile()) {
				file.delete();
			} else if (file.isDirectory()) {
				File subFiles[] = file.listFiles();
				for (File f : subFiles) {
					deleteFile(f);
				}
				if (file.delete()) {
					System.out.println("true");
				} else {
					System.out.println("false");
				}
			}
		}
	}

	/**
	 * 
	 * 
	 * 
	 * @return 执行结果
	 * @throws IOException
	 */
	public String listAllFile() throws IOException {
		if (root.equals("/../")) {
			root = "/";
		}
		File upload = new File(getServletContext().getRealPath(root));
		if (rootFile.getCanonicalPath().equals(upload.getCanonicalPath())) {
			root = "/";
		}
		File files[] = upload.listFiles();
		TreeNode node = null;
		treeNodes = new ArrayList<TreeNode>();
		for (File s : files) {
			node = new TreeNode();
			node.setParent(root);
			node.setDir(s.isDirectory());
			node.setName(s.getName());
			treeNodes.add(node);
		}
		Collections.sort(treeNodes);
		// setStatus(200);
		return "listAllFile";
	}

	/**
	 * 
	 * 列表所有文件
	 * 
	 * @return 执行结果
	 */
	public String listAllDir() {
		File upload = new File(getServletContext().getRealPath(root));
		File files[] = upload.listFiles();
		TreeNode node = new TreeNode();
		node.setDir(true);
		node.setName("...");
		treeNodes = new ArrayList<TreeNode>();
		treeNodes.add(node);
		for (File s : files) {
			if (s.isDirectory()) {
				node = new TreeNode();
				node.setParent(root);
				node.setDir(s.isDirectory());
				node.setName(s.getName());
				treeNodes.add(node);
			}
		}
		return "listAllDir";
	}

	public String cut() {
		if (selects == null) {
			addActionError("EMFM0004");
		} else {
			List<String> files = new ArrayList<String>();
			for (String s : selects) {
				files.add(getServletContext().getRealPath(root)+File.separator + s);
			}
			setSession(CUTFILE, files);
			removeSession(COPYFILE);
		}

		return "cut";
	}

	public String copy() {
		if (selects == null) {
			addActionError("EMFM0005");
		} else {
			List<String> files = new ArrayList<String>();
			for (String s : selects) {
				files.add(getServletContext().getRealPath(root)+File.separator + s);
			}
			setSession(COPYFILE, files);
			removeSession(CUTFILE);
		}

		return "copy";
	}

	@SuppressWarnings("unchecked")
	public String paste() {
		if (getSessionValue(COPYFILE) == null
				&& getSessionValue(CUTFILE) == null) {
			addActionError("EMFM0006");
		} else {
			if (getSessionValue(COPYFILE) != null) {
				List<String> files = (List<String>) getSessionValue(COPYFILE);
				valfile(files);
				if (!hasErrors()) {
					copyFile(files);
				}
				removeSession(COPYFILE);
			} else {
				List<String> files = (List<String>) getSessionValue(CUTFILE);
				valfile(files);
				if (!hasErrors()) {
					copyFile(files);
					for (String s : files) {
						File file = new File(s);
						deleteFile( file);
					}
				}
				removeSession(CUTFILE);
			}
		}
		return "paste";
	}
	private void copyFile(List<String> files){
		File temp;
		for (String s : files) {
			File file = new File(s);
			temp = new File(getServletContext().getRealPath(root)+File.separator + file.getName());
			try {
				FileUtils.copyFile(file, temp);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	private void valfile(List<String> files) {
		File temp;
		for (String s : files) {
			File file = new File(s);
			temp = new File(getServletContext().getRealPath(root)+File.separator + file.getName());
			if (temp.exists()) {
				addActionError("EMFM0001", null, file.getName());
			}
		}
	}
	
	public String getRoot() {
		return root;
	}

	public void setRoot(String root) {
		this.root = root;
	}

	public List<TreeNode> getTreeNodes() {
		return treeNodes;
	}

	public void setTreeNodes(List<TreeNode> treeNodes) {
		this.treeNodes = treeNodes;
	}

	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}

	public List<String> getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}

	public List<String> getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String getModeName() {
		return "fileManager";
	}

	public String[] getSelects() {
		return selects;
	}

	public void setSelects(String selects[]) {
		this.selects = selects;
	}

	@Override
	public Collection<String> getActionErrors() {
		return super.getActionErrors();
	}

	public Boolean getPaste() {
		return getSessionValue(COPYFILE) != null
				|| getSessionValue(CUTFILE) != null;
	}

	@Override
	public Class<T> getEntityClass() {
		return null;
	}
}
